#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         Tody Guo

 Script Function:
	Monitor devices specified

#ce ----------------------------------------------------------------------------

#NoTrayIcon

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <WinAPISys.au3>

Global $res1
Global $isResOk=True
Global $DevIsOk=True
Global $Devcount2=0
Global $Devcount1=0

;; list devices
func initMonDev()
    $Devcount1=0
	$strComputer = "."
	$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
	$colItems = $objWMIService.ExecQuery("Select * from Win32_PnPEntity WHERE ConfigManagerErrorCode = 0")
	FileWrite(@ScriptDir&"\Mondev.log", "=================================================================================="&@CRLF)
	For $objItem in $colItems
		If StringInStr($objItem.Description, "Realtek") Or _
		   StringInStr($objItem.Description, "Wireless") Or _
		   StringInStr($objItem.Description, "Nvidia") _
		   Then
			   FileWrite(@ScriptDir&"\Mondev.log", $objItem.DeviceID & ": " & $objItem.Description&@CRLF)
			   $Devcount1=$Devcount1+1
		EndIf
   Next
   FileWrite(@ScriptDir&"\Mondev.log", "=================================================================================="&@CRLF)
Endfunc

;; list devices
func MonDev()
    $Devcount2=0
	$strComputer = "."
	$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
	$colItems = $objWMIService.ExecQuery("Select * from Win32_PnPEntity WHERE ConfigManagerErrorCode = 0")
	For $objItem in $colItems
		If StringInStr($objItem.Description, "Realtek") Or _
		   StringInStr($objItem.Description, "Wireless") Or _
		   StringInStr($objItem.Description, "Nvidia") _
		   Then
			   GUICtrlSetData($idMylist, $objItem.DeviceID & ": " & $objItem.Description&" - "&$Devcount2)
			   $Devcount2=$Devcount2+1
		EndIf
	Next
Endfunc

Func MonRes()
   $res2=@DesktopWidth&"x"&@DesktopHeight
   GUICtrlSetData($idMylist, " LCD: "& $res1)
   ;;GUICtrlSetData($idMylist, " LCD2: "& $res2)
   If $res1 <> $res2 Then
	  $isResOk=False
   EndIf
EndFunc

Func initFile()
   IniWrite(@ScriptDir&"\setting.ini", "General", "resolution", $res1)
   IniWrite(@ScriptDir&"\setting.ini", "General", "devicenum", $Devcount1)
EndFunc

Func rIniFile()
   $res1=IniRead(@ScriptDir&"\setting.ini", "General", "resolution", "0")
   $Devcount1=IniRead(@ScriptDir&"\setting.ini", "General", "devicenum", "0")
EndFunc

Func Init()
   ;; lcd resolution
   $res1=@DesktopWidth&"x"&@DesktopHeight
   initMonDev()

   If Not FileExists(@ScriptDir&"\setting.ini") Then
	  initFile()
   Else
	  rIniFile()
   EndIf

   GUICtrlSetData($idLabel, " [INI] Dev num: "&$Devcount1&",  "&"LCD resolution: "&$res1)
EndFunc

;; timer for monitors
Func _TimerProc($hWnd, $iMsg, $iTimerID, $iTime)
   MonDev()
   MonRes()

   ;;; check device number
   If $Devcount1 <> $Devcount2 Then
	  $DevIsOk=False
   EndIf

   GUICtrlSetData($idLabe2, "[Real time] Dev status: "&$DevIsOk&",  "&"LCD resolution: "&$isResOk)

   FileWrite(@ScriptDir&"\Mondev.log", "["&@YEAR&"-"&@MON"-"&&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC&"] Dev status: "&$DevIsOk&",  "&"LCD resolution: "&$isResOk&@CRLF)

EndFunc   ;==>_TimerProc

;; GUI begin
Local $hMainGUI = GUICreate("Device monitor     Rev: 1.0.0", 801, 401)
Local $idMylist = GUICtrlCreateList("", 0, 30, 800, 370)
Local $idLabel  = GUICtrlCreateLabel(" [INI] Loading...", 0,9, 350,20)
Local $idLabe2  = GUICtrlCreateLabel("[Real time] Loading...", 310,9, 350,20)
GUICtrlSetData($idMylist, "")
Local $hTimerProc = DllCallbackRegister('_TimerProc', 'none', 'hwnd;uint;uint_ptr;dword')
Local $iTimerID = _WinAPI_SetTimer(0, 0, 1500, DllCallbackGetPtr($hTimerProc))

Init()

GUISwitch($hMainGUI)
GUISetState(@SW_SHOW)

Local $aMsg = 0
While 1
    $aMsg = GUIGetMsg(1)
    Select
        Case $aMsg[0] = $GUI_EVENT_CLOSE And $aMsg[1] = $hMainGUI
            ExitLoop
    EndSelect
WEnd